{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "作业"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "你选择了代码块2\n",
      "1000以内有 168 个素数。\n",
      "这些素数是： [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 接受用户输入\n",
    "user_choice = int(input(\"请输入一个数字（1-10）来选择代码块：\"))\n",
    "\n",
    "# 根据用户输入的数字执行对应的代码块\n",
    "if user_choice == 1:\n",
    "    print(\"你选择了代码块1\")\n",
    "    def is_leap_year(year):\n",
    "        return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)\n",
    "\n",
    "    def day_of_year(year, month, day):\n",
    "        days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "        \n",
    "        if is_leap_year(year):\n",
    "            days_in_month[1] = 29\n",
    "        \n",
    "        day_count = sum(days_in_month[:month - 1]) + day\n",
    "        \n",
    "        return day_count\n",
    "\n",
    "    year = int(input(\"请输入年份: \"))\n",
    "    month = int(input(\"请输入月份: \"))\n",
    "    day = int(input(\"请输入日期: \"))\n",
    "\n",
    "    if month < 1 or month > 12 or day < 1 or day > 31:\n",
    "        print(\"输入的月份或日期不合法，请重新输入。\")\n",
    "    elif day > (31 if month in [1, 3, 5, 7, 8, 10, 12] else 30 if month in [4, 6, 9, 11] else 29 if is_leap_year(year) else 28):\n",
    "        print(\"输入的日期超出该月的天数，请重新输入。\")\n",
    "    else:\n",
    "        result = day_of_year(year, month, day)\n",
    "        print(f\"{year}年{month}月{day}日是这一年的第{result}天。\")\n",
    "elif user_choice == 2:\n",
    "    print(\"你选择了代码块2\")\n",
    "    def is_prime(n):\n",
    "        if n <= 1:\n",
    "            return False\n",
    "        for i in range(2, int(n**0.5) + 1):\n",
    "            if n % i == 0:\n",
    "                return False\n",
    "        return True\n",
    "\n",
    "    def find_primes_up_to(limit):\n",
    "        primes = []\n",
    "        for num in range(2, limit + 1):\n",
    "            if is_prime(num):\n",
    "                primes.append(num)\n",
    "        return primes\n",
    "\n",
    "    limit = 1000\n",
    "    primes = find_primes_up_to(limit)\n",
    "\n",
    "    print(f\"1000以内有 {len(primes)} 个素数。\")\n",
    "    print(\"这些素数是：\", primes)\n",
    "elif user_choice == 3:\n",
    "    print(\"你选择了代码块3\")\n",
    "    def get_grade(score):\n",
    "        if score >= 90:\n",
    "            return 'A'\n",
    "        elif 60 <= score < 90:\n",
    "            return 'B'\n",
    "        else:\n",
    "            return 'C'\n",
    "    scores = [95, 78, 55, 88, 60, 100, 45]\n",
    "    for score in scores:\n",
    "        grade = get_grade(score)\n",
    "        print(f\"成绩 {score} 对应的等级是 {grade}\")\n",
    "elif user_choice == 4:\n",
    "    print(\"你选择了代码块4\")\n",
    "    def count_characters(input_string):\n",
    "        letters = 0\n",
    "        spaces = 0\n",
    "        digits = 0\n",
    "        others = 0\n",
    "\n",
    "        for char in input_string:\n",
    "            if char.isalpha():  \n",
    "                letters += 1\n",
    "            elif char.isspace():  \n",
    "                spaces += 1\n",
    "            elif char.isdigit():  \n",
    "                digits += 1\n",
    "            else:\n",
    "                others += 1\n",
    "\n",
    "        return letters, spaces, digits, others\n",
    "\n",
    "    input_string = input(\"请输入一行字符串：\")\n",
    "\n",
    "    letters, spaces, digits, others = count_characters(input_string)\n",
    "\n",
    "    print(f\"英文字母个数：{letters}\")\n",
    "    print(f\"空格个数：{spaces}\")\n",
    "    print(f\"数字个数：{digits}\")\n",
    "    print(f\"其他字符个数：{others}\")\n",
    "elif user_choice == 5:\n",
    "    print(\"你选择了代码块5\")\n",
    "    def find_perfect_numbers(limit):\n",
    "        perfect_numbers = []\n",
    "\n",
    "        for num in range(2, limit + 1):  \n",
    "            factors = [i for i in range(1, num) if num % i == 0] \n",
    "            if sum(factors) == num:  \n",
    "                perfect_numbers.append(num)\n",
    "\n",
    "        return perfect_numbers\n",
    "\n",
    "    limit = 1000\n",
    "\n",
    "    perfect_numbers = find_perfect_numbers(limit)\n",
    "\n",
    "    print(f\"1000以内的完数有:{perfect_numbers}\")\n",
    "elif user_choice == 6:\n",
    "    print(\"你选择了代码块6\")\n",
    "    def factorial(n):\n",
    "        if n == 0 or n == 1:\n",
    "            return 1\n",
    "        else:\n",
    "            return n * factorial(n - 1)\n",
    "\n",
    "    def sum_of_factorials(N):\n",
    "        total_sum = 0\n",
    "        for i in range(1, N + 1):\n",
    "            total_sum += factorial(i)\n",
    "        return total_sum\n",
    "\n",
    "    N = int(input(\"请输入一个正整数N:\"))\n",
    "    result = sum_of_factorials(N)\n",
    "    print(f\"1到{N}的所有阶乘之和是：{result}\")\n",
    "elif user_choice == 7:\n",
    "    print(\"你选择了代码块7\")\n",
    "    def print_diamond(N):\n",
    "        if N % 2 == 0:\n",
    "            print(\"请输入一个奇数N。\")\n",
    "            return\n",
    "    \n",
    "        for i in range(N // 2 + 1):\n",
    "            for j in range(N // 2 - i):\n",
    "                print(\" \", end=\"\")\n",
    "            for k in range(2 * i + 1):\n",
    "                print(\"*\", end=\"\")\n",
    "            print()\n",
    "    \n",
    "        for i in range(N // 2 - 1, -1, -1):\n",
    "            for j in range(N // 2 - i):\n",
    "                print(\" \", end=\"\")\n",
    "            for k in range(2 * i + 1):\n",
    "                print(\"*\", end=\"\")\n",
    "            print()\n",
    "    N = int(input(\"请输入一个奇数N:\"))\n",
    "    print_diamond(N)\n",
    "elif user_choice == 8:\n",
    "    print(\"你选择了代码块8\")\n",
    "    numbers = [5, 2, 9, 1, 5, 6, 3, 8, 4, 7]\n",
    "\n",
    "    sorted_numbers = sorted(numbers)\n",
    "\n",
    "    print(\"排序后的列表(使用sorted()):\", sorted_numbers)\n",
    "elif user_choice == 9:\n",
    "    print(\"你选择了代码块9\")\n",
    "    sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]\n",
    "\n",
    "    num_to_insert = int(input(\"请输入一个要插入的数：\"))\n",
    "\n",
    "    insert_position = len(sorted_array)\n",
    "\n",
    "    for i in range(len(sorted_array)):\n",
    "        if num_to_insert < sorted_array[i]:\n",
    "            insert_position = i\n",
    "            break\n",
    "\n",
    "    sorted_array.insert(insert_position, num_to_insert)\n",
    "\n",
    "    print(\"插入后的数组：\", sorted_array)\n",
    "elif user_choice == 10:\n",
    "    print(\"你选择了代码块10\")\n",
    "    def josephus_problem(n, k):\n",
    "        people = list(range(n))\n",
    "        index = 0\n",
    "\n",
    "        while len(people) > 1:\n",
    "            index = (index + k - 1) % len(people)\n",
    "            people.pop(index)\n",
    "        return people[0] + 1\n",
    "\n",
    "    n = int(input(\"请输入总人数n:\"))\n",
    "    k = 3  \n",
    "    result = josephus_problem(n, k)\n",
    "    print(f\"最后留下的是原来第{result}号的那位。\")\n",
    "else:\n",
    "    print(\"输入无效，请输入1到10之间的数字。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2024年12月1日是这一年的第336天。\n"
     ]
    }
   ],
   "source": [
    "def is_leap_year(year):\n",
    "    return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)\n",
    "\n",
    "def day_of_year(year, month, day):\n",
    "    days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]\n",
    "    \n",
    "    if is_leap_year(year):\n",
    "        days_in_month[1] = 29\n",
    "    \n",
    "    day_count = sum(days_in_month[:month - 1]) + day\n",
    "    \n",
    "    return day_count\n",
    "\n",
    "year = int(input(\"请输入年份: \"))\n",
    "month = int(input(\"请输入月份: \"))\n",
    "day = int(input(\"请输入日期: \"))\n",
    "\n",
    "if month < 1 or month > 12 or day < 1 or day > 31:\n",
    "    print(\"输入的月份或日期不合法，请重新输入。\")\n",
    "elif day > (31 if month in [1, 3, 5, 7, 8, 10, 12] else 30 if month in [4, 6, 9, 11] else 29 if is_leap_year(year) else 28):\n",
    "    print(\"输入的日期超出该月的天数，请重新输入。\")\n",
    "else:\n",
    "    result = day_of_year(year, month, day)\n",
    "    print(f\"{year}年{month}月{day}日是这一年的第{result}天。\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "dsfsdfs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内有 168 个素数。\n",
      "这些素数是： [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997]\n"
     ]
    }
   ],
   "source": [
    "def is_prime(n):\n",
    "    if n <= 1:\n",
    "        return False\n",
    "    for i in range(2, int(n**0.5) + 1):\n",
    "        if n % i == 0:\n",
    "            return False\n",
    "    return True\n",
    "\n",
    "def find_primes_up_to(limit):\n",
    "    primes = []\n",
    "    for num in range(2, limit + 1):\n",
    "        if is_prime(num):\n",
    "            primes.append(num)\n",
    "    return primes\n",
    "\n",
    "limit = 1000\n",
    "primes = find_primes_up_to(limit)\n",
    "\n",
    "print(f\"1000以内有 {len(primes)} 个素数。\")\n",
    "print(\"这些素数是：\", primes)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "成绩 95 对应的等级是 A\n",
      "成绩 78 对应的等级是 B\n",
      "成绩 55 对应的等级是 C\n",
      "成绩 88 对应的等级是 B\n",
      "成绩 60 对应的等级是 B\n",
      "成绩 100 对应的等级是 A\n",
      "成绩 45 对应的等级是 C\n"
     ]
    }
   ],
   "source": [
    "def get_grade(score):\n",
    "    if score >= 90:\n",
    "        return 'A'\n",
    "    elif 60 <= score < 90:\n",
    "        return 'B'\n",
    "    else:\n",
    "        return 'C'\n",
    "scores = [95, 78, 55, 88, 60, 100, 45]\n",
    "for score in scores:\n",
    "    grade = get_grade(score)\n",
    "    print(f\"成绩 {score} 对应的等级是 {grade}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "英文字母个数：4\n",
      "空格个数：0\n",
      "数字个数：3\n",
      "其他字符个数：1\n"
     ]
    }
   ],
   "source": [
    "def count_characters(input_string):\n",
    "    letters = 0\n",
    "    spaces = 0\n",
    "    digits = 0\n",
    "    others = 0\n",
    "\n",
    "    for char in input_string:\n",
    "        if char.isalpha():  \n",
    "            letters += 1\n",
    "        elif char.isspace():  \n",
    "            spaces += 1\n",
    "        elif char.isdigit():  \n",
    "            digits += 1\n",
    "        else:\n",
    "            others += 1\n",
    "\n",
    "    return letters, spaces, digits, others\n",
    "\n",
    "input_string = input(\"请输入一行字符串：\")\n",
    "\n",
    "letters, spaces, digits, others = count_characters(input_string)\n",
    "\n",
    "print(f\"英文字母个数：{letters}\")\n",
    "print(f\"空格个数：{spaces}\")\n",
    "print(f\"数字个数：{digits}\")\n",
    "print(f\"其他字符个数：{others}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000以内的完数有:[6, 28, 496]\n"
     ]
    }
   ],
   "source": [
    "def find_perfect_numbers(limit):\n",
    "    perfect_numbers = []\n",
    "\n",
    "    for num in range(2, limit + 1):  \n",
    "        factors = [i for i in range(1, num) if num % i == 0] \n",
    "        if sum(factors) == num:  \n",
    "            perfect_numbers.append(num)\n",
    "\n",
    "    return perfect_numbers\n",
    "\n",
    "limit = 1000\n",
    "\n",
    "perfect_numbers = find_perfect_numbers(limit)\n",
    "\n",
    "print(f\"1000以内的完数有:{perfect_numbers}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1到5的所有阶乘之和是：153\n"
     ]
    }
   ],
   "source": [
    "def factorial(n):\n",
    "    if n == 0 or n == 1:\n",
    "        return 1\n",
    "    else:\n",
    "        return n * factorial(n - 1)\n",
    "\n",
    "def sum_of_factorials(N):\n",
    "    total_sum = 0\n",
    "    for i in range(1, N + 1):\n",
    "        total_sum += factorial(i)\n",
    "    return total_sum\n",
    "\n",
    "N = int(input(\"请输入一个正整数N:\"))\n",
    "result = sum_of_factorials(N)\n",
    "print(f\"1到{N}的所有阶乘之和是：{result}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   *\n",
      "  ***\n",
      " *****\n",
      "*******\n",
      " *****\n",
      "  ***\n",
      "   *\n"
     ]
    }
   ],
   "source": [
    "def print_diamond(N):\n",
    "    if N % 2 == 0:\n",
    "        print(\"请输入一个奇数N。\")\n",
    "        return\n",
    "    \n",
    "    for i in range(N // 2 + 1):\n",
    "        for j in range(N // 2 - i):\n",
    "            print(\" \", end=\"\")\n",
    "        for k in range(2 * i + 1):\n",
    "            print(\"*\", end=\"\")\n",
    "        print()\n",
    "    \n",
    "    for i in range(N // 2 - 1, -1, -1):\n",
    "        for j in range(N // 2 - i):\n",
    "            print(\" \", end=\"\")\n",
    "        for k in range(2 * i + 1):\n",
    "            print(\"*\", end=\"\")\n",
    "        print()\n",
    "N = int(input(\"请输入一个奇数N:\"))\n",
    "print_diamond(N)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序后的列表(使用sorted()): [1, 2, 3, 4, 5, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "numbers = [5, 2, 9, 1, 5, 6, 3, 8, 4, 7]\n",
    "\n",
    "sorted_numbers = sorted(numbers)\n",
    "\n",
    "print(\"排序后的列表(使用sorted()):\", sorted_numbers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "插入后的数组： [1, 3, 5, 6, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]\n"
     ]
    }
   ],
   "source": [
    "\n",
    "sorted_array = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25]\n",
    "\n",
    "num_to_insert = int(input(\"请输入一个要插入的数：\"))\n",
    "\n",
    "insert_position = len(sorted_array)\n",
    "\n",
    "for i in range(len(sorted_array)):\n",
    "    if num_to_insert < sorted_array[i]:\n",
    "        insert_position = i\n",
    "        break\n",
    "\n",
    "sorted_array.insert(insert_position, num_to_insert)\n",
    "\n",
    "print(\"插入后的数组：\", sorted_array)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最后留下的是原来第1号的那位。\n"
     ]
    }
   ],
   "source": [
    "def josephus_problem(n, k):\n",
    "\n",
    "    \n",
    "    people = list(range(n))\n",
    "    \n",
    "    \n",
    "    index = 0\n",
    "    \n",
    "   \n",
    "    while len(people) > 1:\n",
    "        \n",
    "        index = (index + k - 1) % len(people)\n",
    "        \n",
    "        people.pop(index)\n",
    "       \n",
    "    return people[0] + 1\n",
    "\n",
    "n = int(input(\"请输入总人数n:\"))\n",
    "k = 3  \n",
    "result = josephus_problem(n, k)\n",
    "print(f\"最后留下的是原来第{result}号的那位。\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
